package awWindowsApplication;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.math.BigInteger;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Random;

import entities.CSFileWrapper;

public class FileIO {
	String folderPath = "/Capstone/";
	String folderName = "Share";
	String folderTemp = "Temp";
	private String folderConfig = "Config";
	
	public FileIO(){
		checkFoldersExist();
	}
	private void checkFoldersExist(){
		System.out.println("FileIO: checkFolderExist()");
		File capFolder = new File(folderPath);
		if(!capFolder.exists()){
			System.out.println("Creating Capstone Folder");
			capFolder.mkdir();
		}
		File shareFolder = new File(folderPath + folderName);
		if(!shareFolder.exists()){
			System.out.println("Creating Share Folder");
			shareFolder.mkdir();
		}
		File tempFolder = new File(folderPath + folderTemp);
		if(!tempFolder.exists()){
			System.out.println("Creating Temp Folder");
			tempFolder.mkdir();
		}
	}
	public CSFileWrapper getFile(String fileName){
		System.out.println("FileIO: getFile()");
		CSFileWrapper file = null;
		
		try {
			Path p = Paths.get(folderPath + folderName + "/" + fileName);
			byte[] data = Files.readAllBytes(p);
			System.out.println("DATA LENGTH " + data.length);
			file = new CSFileWrapper(data, fileName);
		} catch (FileNotFoundException e) {e.printStackTrace();
		} catch (IOException e) {e.printStackTrace();
		}
		
		return file;
	}

	public void writeFile(CSFileWrapper file){	
		System.out.println("FileIO: writeFile()");
		try{
			FileOutputStream fos = new FileOutputStream(folderPath + folderName + "/" + file.getFileName());
			fos.write(file.getData());
			fos.close();
		}catch(Exception ex){ex.printStackTrace();}
	}
	public void writePreviewFile(CSFileWrapper file){
		System.out.println("FileIO: writePreviewFile()");
		try{
			FileOutputStream fos = new FileOutputStream(folderPath + folderTemp + "/" + file.getFileName());
			fos.write(file.getData());
			fos.close();
		}catch(IOException ex){
			ex.printStackTrace();
		}
	}
	public void cleanTempFolder(){
		File file = new File("/Capstone/Temp");
		if(file.isDirectory()){
			String[] myFiles = file.list();
			for(String s : myFiles){
				File myFile = new File(file, s);
				myFile.delete();
			}
		}
	}
	
	public ArrayList<String> getFileNames(){
		System.out.println("FileIO: getFileNames()");
		ArrayList<String> fileNames = new ArrayList<String>();
		
		File share = new File(folderPath + folderName);
		if(share != null){
			if(share.listFiles() != null){
				for(File fileEntry : share.listFiles()){
						fileNames.add(fileEntry.getName());
				}
			}
		}
		
		return fileNames;
	}
	
	//Hidden folder w/ config file
	public void createHiddenFolder(){
		File conDir = new File(folderPath + folderConfig);
		if(!conDir.exists()){
			conDir.mkdir();
			makeHidden(conDir);
		}
		genConfigFile();
	}
	private void makeHidden(File path){
		URI uri = path.toURI();
		Path file = Paths.get(uri);
		try{
			Files.setAttribute(file, "dos:hidden", true);
		}catch(IOException ex){ex.printStackTrace();
		}
	}
	public ArrayList<String> getConfigFile(){
		File file = new File(folderPath + folderConfig + "/waconfig.txt");
		ArrayList<String> config = new ArrayList<String>();
		try {
			BufferedReader in = new BufferedReader(new FileReader(file));
			String configLine = "";
			while((configLine = in.readLine()) != null){
				config.add(configLine);
			}
			in.close();
		} catch (FileNotFoundException e) {e.printStackTrace();
		} catch (IOException e) {e.printStackTrace();
		}
		
		return config;
	}
	public void updateConfigFile(ArrayList<String> config){
		File conFile = new File(folderPath + folderConfig + "/waconfig.txt");
		try{
			BufferedWriter out = new BufferedWriter(new FileWriter(conFile, false));
			for(String s : config){
				out.write(s);
				out.newLine();
			}
			out.close();
		}catch(IOException ex){ex.printStackTrace();
		}
	}
	private void genConfigFile(){
		File conFile = new File(folderPath + folderConfig + "/waconfig.txt");
		if(!conFile.exists()){			
			String initData = "";
			//RSAKey initial
			initData += "rsaexp:";			
			Random rand = new Random();
			BigInteger exp = BigInteger.valueOf((rand.nextInt(89999)+10000));			
			initData += exp;
			
			//		
			try {
				conFile.createNewFile();
				
				BufferedWriter out = new BufferedWriter(new FileWriter(conFile, false));
				out.write(initData);
				out.newLine();
				out.close();
			} catch (IOException e) {e.printStackTrace();
			}
			
//			makeHidden(conFile);
		}
	}
}
